package com.example.tellwin.home.adpater;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.tellwin.R;
import com.example.tellwin.home.bean.TeactherTeamBean;
import com.example.tellwin.utils.CommonUtils;
import com.example.tellwin.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class TeactherTeamAdpater extends RecyclerView.Adapter<TeactherTeamAdpater.TeacherTeamViewHolder> {

    private List<TeactherTeamBean> beans = new ArrayList<>();
    Context context;
    private OnTeacherItemClickListener onTeacherItemClickListener;

    public void setOnTeacherItemClickListener(OnTeacherItemClickListener onTeacherItemClickListener) {
        this.onTeacherItemClickListener = onTeacherItemClickListener;
    }

    public void setBeans(List<TeactherTeamBean> beans) {
        this.beans = beans;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public TeacherTeamViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new TeacherTeamViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_teacher_team, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull TeacherTeamViewHolder holder, int position) {
        TeactherTeamBean bean = beans.get(position);
        if (position % 3 == 0) {
            holder.view.setBackgroundResource(R.drawable.yellow_corners_shape);
        } else if (position % 3 == 1) {
            holder.view.setBackgroundResource(R.drawable.green_corners_6_shape);
        } else {
            holder.view.setBackgroundResource(R.drawable.blue_corners_shape);
        }
        GlideUtils.loadHeadImage(context, bean.getHeadPic(), holder.teacherHeadIv);
        holder.teacherNameTv.setText(bean.getRealName());
        holder.teacherTypeTv1.setText(CommonUtils.getStr(bean.getLevelName()));
        holder.teacherTypeTv2.setText(bean.getSchool());
        holder.teacherTypeTv3.setText(bean.getSubjectName());
        holder.view.setOnClickListener(view -> {
            if (onTeacherItemClickListener != null) {
                onTeacherItemClickListener.onTeacherItemClick(position, bean);
            }
        });
    }

    @Override
    public int getItemCount() {
        return beans.size();
    }

    class TeacherTeamViewHolder extends RecyclerView.ViewHolder {
        LinearLayout view;
        ImageView teacherHeadIv;
        TextView teacherNameTv;
        TextView teacherTypeTv1;
        TextView teacherTypeTv2;
        TextView teacherTypeTv3;

        public TeacherTeamViewHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView.findViewById(R.id.item_teacher_team_view);
            teacherHeadIv = (ImageView) itemView.findViewById(R.id.teacher_head_iv);
            teacherNameTv = (TextView) itemView.findViewById(R.id.teacher_name_tv);
            teacherTypeTv1 = (TextView) itemView.findViewById(R.id.teacher_type_tv1);
            teacherTypeTv2 = (TextView) itemView.findViewById(R.id.teacher_type_tv2);
            teacherTypeTv3 = (TextView) itemView.findViewById(R.id.teacher_type_tv3);
        }
    }

    public interface OnTeacherItemClickListener {
        void onTeacherItemClick(int position, TeactherTeamBean teactherTeamBean);
    }
}
